package com.hongyuan.sellcoach.ui.main.activity;

import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;

import androidx.lifecycle.ViewModelProviders;

import com.hongyuan.sellcoach.R;
import com.hongyuan.sellcoach.base.AppViewModelFactory;
import com.hongyuan.sellcoach.base.CustomActivity;
import com.hongyuan.sellcoach.databinding.ActivityRetrievePasswordBinding;
import com.hongyuan.sellcoach.ui.main.view_mode.RetrievePasswordViewModel;
import com.hongyuan.sellcoach.utils.CustomDialog;

import me.tatarka.bindingcollectionadapter2.BR;

public class RetrievePasswordActivity extends CustomActivity<ActivityRetrievePasswordBinding, RetrievePasswordViewModel> {

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_retrieve_password;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initView() {
        super.initView();

        //开启拖动效果
        setOnRefresh(binding.refresh,REFRESH_0X4);
    }

    @Override
    public RetrievePasswordViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(RetrievePasswordViewModel.class);
    }

    @Override
    public void initViewObservable() {
        //监听ViewModel中pSwitchObservable的变化, 当ViewModel中执行【uc.pSwitchObservable.set(!uc.pSwitchObservable.get());】时会回调该方法
        viewModel.uc.pSwitchEvent.observe(this, aBoolean -> {
            //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
            if (viewModel.uc.pSwitchEvent.getValue()) {
                //密码可见
                //在xml中定义id后,使用binding可以直接拿到这个view的引用,不再需要findViewById去找控件了
                binding.ivSwichPasswrod.setImageResource(R.mipmap.vtwo_login_eye_outline_img_);
                binding.etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            } else {
                //密码不可见
                binding.ivSwichPasswrod.setImageResource(R.mipmap.vtwo_login_eye_off_img_);
                binding.etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        });

        viewModel.uc.showSuccess.observe(this,aBoolean -> {
            CustomDialog.promptDialog(this, "密码已修改成功！", "好的!", "谢谢！", true, v -> {
                finish();
            });
        });
    }
}
